from vuer import Vuer, VuerSession
from vuer.schemas import DefaultScene, Movable, Gripper
from asyncio import sleep

app = Vuer(host='0.0.0.0', cert="cert.pem", key="key.pem", queries=dict(grid=False), queue_len=3)

@app.add_handler("OBJECT_MOVE")
async def handler(event, session):
    print(f"Movement Event: key-{event.key}", event.value)

# @app.add_handler("HAND_MOVE")
# async def handler(event, session):
#     '''
#     该事件内容是空的
#     '''
#     print(f"Movement Event: key-{event.key}", event.value)

# @app.add_handler("CONTROLLER_MOVE")
# async def handler(event, session):
#     '''
#     该事件内容是手柄的位姿
#     '''
#     print(f"------Movement Event: key-{event.key}", event.value)

@app.spawn(start=True)
async def main(session: VuerSession):
    session.set @ DefaultScene()

    session.upsert @ Movable(Gripper(key="not-moving"))
    import numpy as np

    for i in range(10000):
        # We move the gripper at a const speed around the origin.
        x = np.sin(i / 60) / 2
        y = np.cos(i / 60) / 2
        session.upsert @ Movable(Gripper(), position=[x, y, 0], key='moving-one')
        await sleep(0.016)

    while True:
        await sleep(1)